package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.query.QueryWrapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.EquipmentAddDto;
import com.one.group.model.dto.EquipmentQueryDto;
import com.one.group.model.dto.EquipmentUpdateDto;
import com.one.group.model.entity.Equipment;
import com.one.group.model.entity.MaintenanceRecord;
import com.one.group.model.entity.MaintenanceUnitInfo;
import com.one.group.model.entity.RepairRecord;
import com.one.group.model.vo.EquipmentInfoVo;
import com.one.group.model.vo.EquipmentVo;
import com.one.group.result.Result;
import com.one.group.service.EquipmentService;
import com.one.group.service.MaintenanceRecordService;
import com.one.group.service.MaintenanceUnitInfoService;
import com.one.group.service.RepairRecordService;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.ThrowUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * {@code @Create:}  2025-09-28  14 : 30
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */

@SuppressWarnings({"all"})
@RestController
@RequestMapping("/equipment")
@Slf4j
@Tag(name = "设备接口")
public class EquipmentController {
    @Resource
    private EquipmentService equipmentService;

    @Resource
    private RepairRecordService repairRecordService;

    @Resource
    private MaintenanceRecordService maintenanceRecordService;

    @Resource
    private MaintenanceUnitInfoService maintenanceUnitInfoService;

    @GetMapping("/info")
    @Operation(summary = "设备详情接口")
    public Result getInfo(@RequestParam Long id) {
        System.out.println("当前登录状态: " + StpUtil.isLogin());
        System.out.println("当前登录ID: " + StpUtil.getLoginIdDefaultNull());
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        QueryWrapper wrapper = new QueryWrapper().eq(Equipment::getId, id);
        Equipment equipment = equipmentService.getOne(wrapper);
        return equipment != null ? ResultUtil.success(equipment) : ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
    }


    @PostMapping("/list")
    @Operation(summary = "设备列表接口")
    public Result list(@RequestBody EquipmentQueryDto equipmentQueryDto) {

        new PageHelper().startPage(equipmentQueryDto.getPage(), equipmentQueryDto.getLimit());

        List<EquipmentVo> equipmentVoList = equipmentService.findEquipmentByQueryDto(equipmentQueryDto);

        PageInfo<EquipmentVo> equipmentVoPageInfo = new PageInfo<>(equipmentVoList);

        return ResultUtil.success(equipmentVoPageInfo);
    }

    @PostMapping("/add")
    @Operation(summary = "设备添加接口")
    public Result add(@RequestBody EquipmentAddDto equipmentAddDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(equipmentAddDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = equipmentService.addEquipmentByAddDto(equipmentAddDto);
        return result ? ResultUtil.success("新增成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @PutMapping("/update")
    @Operation(summary = "设备修改接口")
    public Result update(@RequestBody EquipmentUpdateDto equipmentUpdateDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(equipmentUpdateDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = equipmentService.updateEquipmentByUpdateDto(equipmentUpdateDto);
        return result ? ResultUtil.success("修改成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "设备删除接口")
    public Result delete(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = equipmentService.deleteEquipmentByIds(id);
        return result ? ResultUtil.success("删除成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @DeleteMapping("/deleteByBatch")
    @Operation(summary = "设备批量删除接口")
    public Result delete(@RequestBody BaseDeleteDto baseDeleteDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(baseDeleteDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = equipmentService.deleteEquipmentWithMantenanceByIds(baseDeleteDto.getIds());
        return result ? ResultUtil.success("删除成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }


    @GetMapping("/getEquipmentInfoById")
    @Operation(summary = "设备详情基本信息接口")
    public Result getEquipmentInfoById(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        EquipmentInfoVo equipment = equipmentService.getOneInfoById(id);
        return equipment != null ? ResultUtil.success(equipment) : ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
    }

    @GetMapping("/getRepairRecordByEquipmentId")
    @Operation(summary = "设备维修记录接口")
    public Result getRepairRecordByEquipmentId(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        QueryWrapper wrapper = new QueryWrapper().eq(RepairRecord::getEquipmentId, id);
        RepairRecord equipmentList = repairRecordService.getOne(wrapper);
        return equipmentList != null ? ResultUtil.success(equipmentList) : ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
    }

    @GetMapping("/getMaintenanceRecordByEquipmentId")
    @Operation(summary = "设备保养记录接口")
    public Result getMaintenanceRecordByEquipmentId(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        QueryWrapper wrapper = new QueryWrapper().eq(MaintenanceRecord::getEquipmentId, id);
        MaintenanceRecord equipmentList = maintenanceRecordService.getOne(wrapper);
        return equipmentList != null ? ResultUtil.success(equipmentList) : ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
    }

    @GetMapping("/getMaintenanceUnitInfoByEquipmentId")
    @Operation(summary = "设备维保单位接口")
    public Result getMaintenanceUnitInfoByEquipmentId(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        QueryWrapper wrapper = new QueryWrapper().eq(MaintenanceUnitInfo::getEquipmentId, id);
        MaintenanceUnitInfo equipmentList = maintenanceUnitInfoService.getOne(wrapper);
        return equipmentList != null ? ResultUtil.success(equipmentList) : ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
    }
}
